package com.aigou.admin.controller;

import com.aigou.admin.entity.PageEntity;
import com.aigou.admin.entity.Product;
import com.aigou.admin.entity.ResponseEntity;
import com.aigou.admin.service.ProductService;
import com.aigou.admin.service.impl.ProductServiceImpl;
import com.alibaba.fastjson2.JSON;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.List;

@WebServlet("/product")
public class ProductController extends BaseServlet {

    private final ProductService productService = new ProductServiceImpl();

    // 将方法名称作为参数传递  m=save

    // http://localhost:8080/product?m=findAll
    public void findAll(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        // 查询所有产品
        List<Product> products = productService.getAllProducts();
        // 转换为JSON,通过流发送给客户端
        resp.getWriter().write(JSON.toJSONString(new ResponseEntity("OK", products)));
    }

    // http://localhost:8080/product?m=findById&id=1
    public void findById(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        // 获得产品id
        String id = req.getParameter("id");
        // 查询产品
        Product product = productService.getProductById(id);
        // 转换为JSON,通过流发送给客户端
        resp.getWriter().write(JSON.toJSONString(new ResponseEntity("OK", product)));
    }

    // http://localhost:8080/product?m=save&id=50000&product_name=华为电脑&product_image=电脑图片&price=5000&product_type=电脑&product_desc=hhhh&create_time=2024-12-07 22:58:00&product_brand=华为
    public void save(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String id = req.getParameter("id");
        String productName = req.getParameter("product_name");
        String productImage = req.getParameter("product_image");
        double price = Double.parseDouble(req.getParameter("price"));
        String productType = req.getParameter("product_type");
        String productDesc = req.getParameter("product_desc");
        Timestamp createTime = Timestamp.valueOf(req.getParameter("create_time"));
        String productBrand = req.getParameter("product_brand");

        Product product = new Product(id, productName, productImage, price, productType, productDesc, createTime, productBrand);
        // 保存产品
        int rs = productService.addProduct(product);
        resp.getWriter().write(JSON.toJSONString(new ResponseEntity("OK", rs)));
    }

    // http://localhost:8080/product?m=delete&id=50000
    public void delete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String id = req.getParameter("id");
        // 删除产品
        int rs = productService.deleteProduct(id);
        resp.getWriter().write(JSON.toJSONString(new ResponseEntity("OK", rs)));
    }

    // http://localhost:8080/product?m=update&id=50000&product_name=华为电脑&product_image=电脑图片&price=5000&product_type=电脑&product_desc=hhhh&create_time=2024-12-07 22:58:00&product_brand=华为
    public void update(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String id = req.getParameter("id");
        String productName = req.getParameter("product_name");
        String productImage = req.getParameter("product_image"); // 添加对product_image的处理
        double price = Double.parseDouble(req.getParameter("price"));
        String productType = req.getParameter("product_type");
        String productDesc = req.getParameter("product_desc");
        Timestamp createTime = Timestamp.valueOf(req.getParameter("create_time"));
        String productBrand = req.getParameter("product_brand");

        Product product = new Product(id, productName, productImage, price, productType, productDesc, createTime, productBrand);
        // 更新产品
        int rs = productService.updateProduct(product);
        resp.getWriter().write(JSON.toJSONString(new ResponseEntity("OK", rs)));
    }

    // http://localhost:8080/product?m=page&current=1&pageSize=5
    public void page(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String current = req.getParameter("current");
        String pageSize = req.getParameter("pageSize");
        // 查询用户
        PageEntity<Product> pageEntity = productService.findProductPage(Integer.valueOf(current), Integer.valueOf(pageSize));
        // 转换为JSON,通过流发送给客户端
        ResponseEntity.ok(resp, pageEntity);
    }
}
